package com.sokal.easycare.modules.contract.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.sokal.easycare.common.config.Global;
import com.sokal.easycare.common.persistence.Page;
import com.sokal.easycare.common.web.BaseController;
import com.sokal.easycare.common.utils.StringUtils;
import com.sokal.easycare.modules.contract.entity.ContractInfo;
import com.sokal.easycare.modules.contract.service.ContractInfoService;

/**
 * 合同信息Controller
 * @author liliangdong
 * @version 2017-04-10
 */
@Controller
@RequestMapping(value = "${adminPath}/contract/contractInfo")
public class ContractInfoController extends BaseController {

	@Autowired
	private ContractInfoService contractInfoService;
	
	@ModelAttribute
	public ContractInfo get(@RequestParam(required=false) String id) {
		ContractInfo entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = contractInfoService.get(id);
		}
		if (entity == null){
			entity = new ContractInfo();
		}
		return entity;
	}
	
	@RequiresPermissions("contract:contractInfo:view")
	@RequestMapping(value = {"list", ""})
	public String list(ContractInfo contractInfo, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<ContractInfo> page = contractInfoService.findPage(new Page<ContractInfo>(request, response), contractInfo); 
		model.addAttribute("page", page);
		return "modules/contract/contractInfoList";
	}

	@RequiresPermissions("contract:contractInfo:view")
	@RequestMapping(value = "form")
	public String form(ContractInfo contractInfo, Model model) {
		model.addAttribute("contractInfo", contractInfo);
		return "modules/contract/contractInfoForm";
	}

	@RequiresPermissions("contract:contractInfo:edit")
	@RequestMapping(value = "save")
	public String save(ContractInfo contractInfo, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, contractInfo)){
			return form(contractInfo, model);
		}
		contractInfoService.save(contractInfo);
		addMessage(redirectAttributes, "保存合同信息成功");
		return "redirect:"+Global.getAdminPath()+"/contract/contractInfo/?repage";
	}
	
	@RequiresPermissions("contract:contractInfo:edit")
	@RequestMapping(value = "delete")
	public String delete(ContractInfo contractInfo, RedirectAttributes redirectAttributes) {
		contractInfoService.delete(contractInfo);
		addMessage(redirectAttributes, "删除合同信息成功");
		return "redirect:"+Global.getAdminPath()+"/contract/contractInfo/?repage";
	}

}